#include "client_login.h"

Client_login::Client_login(QWidget *parent) : QMainWindow(parent)
{
    setWindowTitle("Client Login");
    resize(350,300);
    this->setStyleSheet("background-color: rgb(198,252,252)");

    myWidget = new QWidget;
    UserNameLabel = new QLabel("用户名:");
    UserNameLineEdit = new QLineEdit;
    UserNameLineEdit->setStyleSheet("background-color: rgb(250,250,250)");
    UserPasswdLabel = new QLabel("密码:");
    UserPasswdLineEdit = new QLineEdit;
    UserPasswdLineEdit->setStyleSheet("background-color: rgb(250,250,250)");
    ServerIPLabel = new QLabel("服务器IP:");
    ServerIPLineEdit = new QLineEdit("100.64.173.40");
    ServerIPLineEdit->setStyleSheet("background-color: rgb(250,250,250)");
    PortLabel = new QLabel("端口:");
    PortLineEdit = new QLineEdit;
    PortLineEdit->setStyleSheet("background-color: rgb(250,250,250)");
    EnterBtn = new QPushButton("登陆");
    EnterBtn->setStyleSheet("background-color: rgb(135,243,246)");
    SignBtn = new QPushButton("注册");
    SignBtn->setStyleSheet("background-color: rgb(135,243,246)");

    layout=new QGridLayout();

    layout->addWidget(UserNameLabel,2,0);
    layout->addWidget(UserNameLineEdit,2,1);
    layout->addWidget(UserPasswdLabel,3,0);
    layout->addWidget(UserPasswdLineEdit,3,1);
    layout->addWidget(ServerIPLabel,4,0);
    layout->addWidget(ServerIPLineEdit,4,1);
    layout->addWidget(PortLabel,5,0);
    layout->addWidget(PortLineEdit,5,1);
    layout->addWidget(EnterBtn,6,0,1,2);
    layout->addWidget(SignBtn,7,0,1,2);
    myWidget->setLayout(layout);
    setCentralWidget(myWidget);


    status = false;
    port = 8010;
    PortLineEdit->setText(QString::number(port));

    ServerIP = new QHostAddress;

    connect(EnterBtn,SIGNAL(clicked()),this,SLOT(slotEnter()));//登录按钮与登录函数绑定
    connect(SignBtn,SIGNAL(clicked()),this,SLOT(slotSignUp()));//注册按钮与注册函数绑定
}

void Client_login::slotEnter(){
    tcpClient = new MainWindow(this);
    tcpClient->show();
    this->hide();
    connect(this, &Client_login::enter, tcpClient, &MainWindow::slotEnter);
    connect(this, &Client_login::store, tcpClient, &MainWindow::store);
    connect(tcpClient, &MainWindow::close, this, &Client_login::close);
    emit enter(ServerIPLineEdit->text(), UserNameLineEdit->text(), UserPasswdLineEdit->text());
    emit store(ServerIPLineEdit->text(), UserNameLineEdit->text(), UserPasswdLineEdit->text());
//    if(!status){//与注册基本相同
//        QString ip = ServerIPLineEdit->text();
//        if(!ServerIP->setAddress(ip)){
//            QMessageBox::information(this,"wrong","server ip error");
//            return ;
//        }
//        if(UserNameLineEdit->text() == ""){
//            QMessageBox::information(this,"wrong","user name error");
//            return ;
//        }
//        UserName = UserNameLineEdit->text();
//        qDebug()<<"slotEnter进来了！";

//        tcpSocket = new QTcpSocket(this);
//        connect(tcpSocket,SIGNAL(connected()),this,SLOT(slotConnected()));
//        connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));
//        connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(DataReceived()));

//        tcpSocket->connectToHost(*ServerIP,port);
//        status = true;
//    }
//    else{//如果tcp连接状态为true，即已登入，用户现在的操作是注销
//        int length = 0;
//        QString msg = UserName + ": leave chat room";
//        FormatMsg(msg, OTHERMSG);//向服务器发送用户退出的信息
//        if((length = tcpSocket->write(msg.toLatin1(),msg.length())) != msg.length()){
//            return;
//        }
//        tcpSocket->disconnectFromHost();
//        status = false;
//        delete p2pserver;//删除该用户的p2p服务
//    }
}

void Client_login::slotSignUp(){//用于注册的槽函数
    if(!status){//如果还未建立tcp连接，就进行下列操作
        QString ip = ServerIPLineEdit->text();//获取服务器主机的ip地址
        if(!ServerIP->setAddress(ip)){//校验IP地址格式是否正确
            QMessageBox::information(this,"wrong","server ip error");
            return ;
        }
        if(UserNameLineEdit->text() == ""){//校验姓名
            QMessageBox::information(this,"wrong","user name error");
            return ;
        }
        if(UserPasswdLineEdit->text() == ""){//校验密码是否为空
            QMessageBox::information(this,"wrong","pass word error");
            return ;
        }
        UserName = UserNameLineEdit->text();//获取姓名
        QString passwd = UserPasswdLineEdit->text();//获取密码

        tcpSocket = new QTcpSocket(this);//新建套接字实例
        connect(tcpSocket,SIGNAL(connected()),this,SLOT(SignConnected()));//连接上后，调用注册业务的函数
        connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(SignDisconnected()));//断开连接时，调用注册断开函数
        connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(SignDataReceived()));//该套接字收到信息后，调用注册信息接收函数

        tcpSocket->connectToHost(*ServerIP,port);//与服务器建立套接字连接
        status = true;//将tcp连接状态设置为已连接
    }
}

void Client_login::FormatMsg(QString &msg, MsgKind kind){
    qint64 len = msg.length();
    QString s = QString::number(len);
    if(len > 100000){//消息长度过大，信息设置为错误，限制单次传输的数据量
        msg = "error|||";
        return;
    }
    for(int i = s.length(); i < 6; ++i){//将消息头统一为固定长度的字符串
        s = "0" + s;
    }
    switch (kind) {
    case NAMEMSG:
        msg = "NM" + s + msg;
        break;
    case METAMSG:
        msg = "MM" + s + msg;
        break;
    case DISCONNECT:
        msg = "DC" + s + msg;
        break;
    case SHAREMSG:
        msg = "SM" + s + msg;
        break;
    case DELETEMSG:
        msg = "DM" + s + msg;
        break;
    case SEARCHMSG:
        msg = "SR" + s + msg;
        break;
    case SIGNMSG:
        msg = "GM" + s + msg;
        break;
    case OTHERMSG:
        msg = "OT" + s + msg;
        break;
    case BREAKPOINT://向服务器查询断点文件
        msg = "BP" + s + msg;
        break;
    case UPDATELOG://向服务器更新断点日志
        msg = "UL" + s + msg;
        break;
    }
}


void Client_login::slotConnected(){
    tcpClient = new MainWindow(this);
    tcpClient->show();
    this->hide();
    connect(this, &Client_login::enter, tcpClient, &MainWindow::slotConnected);
//    emit enter();
}

void Client_login::close(){
    qDebug()<<"close进来了";
    tcpClient->hide();
    this->show();
}
